//是否登录
import { is_signin } from '@/api/API_sign_in_out'
// 引入路由组件
// 主页
import Home from '@/views/Home/Home.vue'
// 编程内容管理
import Pc_Directory_Tree_Management from '@/views/Programming_content/Directory_Tree_Management.vue'
import Pc_Article_management from '@/views/Programming_content/Article_management.vue'
import Pc_Uploaddocument from '@/views/Programming_content/Uploaddocument.vue'
// 其他内容管理
import Oc_Directory_Tree_Management from '@/views/other_content/Directory_Tree_Management.vue'
import Oc_Article_management from '@/views/other_content/Article_management.vue'
import Oc_Uploaddocument from '@/views/other_content/Uploaddocument.vue'
// 链接管理
import Linkmanagement from '@/views/Linkmanagement/Linkmanagement.vue'
// 用户管理
import RemakePassword from '@/views/UsersManage/RemakePassword.vue'
// 核心发展
import Coreliteracy from '@/views/Coreliteracy/Coreliteracy.vue'
// 我的动态
import Dynamic from '@/views/Dynamic/Dynamic.vue'
// 登录页面
import Login from '@/views/Login/Login.vue'
// 主页管理
import Homepage from '@/views/HomepageManage/Homepage.vue'
// 数据导出
import Export_data from '@/views/Export_data/Export_data.vue'
// 倒数日
import Countdown from '@/views/Countdown/Countdown.vue'
// import test from '@/test.vue'

const routes = [
  // 登录
  { path: '/login', component: Login },
  { path: '/', redirect: '/home/pc_dtm' },
  {
    path: '/home',
    component: Home,
    children: [
      // home 子路由
      // 编程内容管理
      { path: 'pc_am_up/:useModel', component: Pc_Uploaddocument, props: true },
      {
        path: 'pc_am_new/:useModel',
        component: Pc_Article_management,
        props: true
      },
      { path: 'pc_dtm', component: Pc_Directory_Tree_Management },
      // 其他内容管理
      { path: 'oc_am_up/:useModel', component: Oc_Uploaddocument, props: true },
      {
        path: 'oc_am_new/:useModel',
        component: Oc_Article_management,
        props: true
      },
      { path: 'oc_dtm', component: Oc_Directory_Tree_Management },
      // 链接管理
      { path: 'lm', component: Linkmanagement },
      // 用户管理
      { path: 'um', component: RemakePassword },
      // 核心发展
      { path: 'cl', component: Coreliteracy },
      // 我的动态
      { path: 'dnm', component: Dynamic },
      // 主页
      { path: 'hp', component: Homepage },
      // 数据导出
      { path: 'ed', component: Export_data },
      { path: 'cd', component: Countdown }
    ]
  }
]
import { createRouter, createWebHistory } from 'vue-router'

const routerHistory = createWebHistory()

const router = createRouter({
  mode:'hash',
  history: routerHistory,
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 只有登录页面可以直接放行
  if (to.path == '/login') {
    next()
  } else {
    // 判断是否登录
    is_signin(({ data: res }) => {
      // 未登录
      if (res.ret != 0) {
        alert(res.msg)
        // 重定向到登录页面
        next('/login')
        // next()
      } else {
        next()
      }
    })
  }
})

export default router
